var TodoTask = function(){
	var remindable = false; 
	var submitForm = function(formId,url) {
		var formObj = document.getElementById(formId);
		formObj.action = url;
		$(':submit').disabled = true;
		formObj.submit();
	};
	var request = function(url){
		document.location.href=url;
	};
	var remindStr = new Array();
	remindStr["startTask"] = "确认开始任务？，请确认在足够短时间内完成（快速反馈,尽早修正）";
	remindStr["compleTask"] = "重构了么？重复！表达力！单一！";
	
	var confirmRemind = function(key){
		if(remindable)
			return confirm(remindStr[key]);
		return true;
	};
	return {
		focusStarting : function(){
			$("#addNewTask").focus();
			$("#focusStarting").focus();
		},
		saveAddTaskListing : function(formId){
			var url = "TodoTask.do?m=saveTaskListing";
			submitForm(formId,url);
			return false;
		},
		saveAddTaskOne : function(formId){
			if(!$('#addNewTask').val())
				return;
			var url = "TodoTask.do?m=saveTaskOne";
			submitForm(formId,url);
			return false;
		},
		openTaskListing : function(tlId){
			var url = "TodoTask.do?m=openTaskListing";
			if(tlId)
				url = url + "&id="+tlId;
			//myJq.updateDiv("myTmpDiv",url);
			myJq.ajaxOpen(url,250,250);
		},
		toggleTkList : function(tkId) {
			$('#'+tkId).toggle();
		},
		showTaskListing : function(tkId) {
			var url = "TodoTask.do?m=showTaskListing&taskListingID="+tkId;
			request(url);
		},
		startOrStopTaskOne : function(tkId,isStart) {
			if(!isStart)
				if(!confirmRemind('startTask'))
					return;
			var url = "TodoTask.do?m=startOrStopTaskOne&taskOneID="+tkId;
			request(url);
		},
		startOrStopTaskListing : function(tkId) {
			var url = "TodoTask.do?m=startOrStopTaskListing&taskListingID="+tkId;
			request(url);
		},
		delTaskOne : function(tkId,dqListId){
			if(!confirm("确认删除？"))
				return;
			var url = "TodoTask.do?m=delTaskOne&id="+tkId+"&pId="+dqListId;
			request(url);
		},
		delTaskListing : function(tkId) {
			if(!confirm("确认删除当前列表，以及其下所有Task吗？"))
				return;
			var url = "TodoTask.do?m=delTaskListing&taskListingId="+tkId;
			request(url);
		},
		toggleCompleted : function(tkId,dqListId,completed) {
			if("completed" != completed)
				if(!confirmRemind('compleTask'))
					return;
			var url = "TodoTask.do?m=toggleCompleted&status=completed&id="+tkId+"&pId="+dqListId;
			request(url);
		}
	}
}();
var myJq = function(){
	var tmpDivId = "myTmpDiv";
	var addStamp = function(url){
		var timeStamp = "timeStamp="+Math.random(); 
		if(url.indexOf("?") < 0)
			timeStamp = "?"+timeStamp;
		else
			timeStamp = "&"+timeStamp;
		return url + timeStamp;
	};
	return {
		updateDiv : function(divId,url) {
			url = addStamp(url);
			$("#"+divId).load(url); 
		},
		ajaxOpen : function(url,w,h){
			this.openDiv(tmpDivId,w,h);
			this.updateDiv(tmpDivId,url);
		},
		openDiv : function(divId,w,h){
			$('#'+divId).dialog({
				autoOpen: true,
				height: h,
				width: w,
				modal: true,
				resizable:false
			});
		}
	}
}();